﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using NUnit.Framework;
using fbasync;
using fbasync.webcomponents;
using System.Web;

namespace fbasync.tests
{
    //class to test utility.End
    //TODO: refactor out to own module


    [TestFixture]
    public class UtilityTests
    {
        XmlDocument XMLDoc;

        [SetUp]
        public void Init()
        {
            XMLDoc = new XmlDocument();
            XMLDoc.Load("../../test.xml");
        }

        [Test]
        public void AsyncEnd()
        {

        }

        [Test]
        public void GetChildText()
        {
            XmlElement XMLElement = (XmlElement)XMLDoc.DocumentElement.FirstChild;

            string sLastName = XMLElement.GetChildText("last_name");
            string sNullString = XMLElement.GetChildText("bogus_node");
            string sEmpty = XMLElement.GetChildText("empty_node");

            Assert.AreEqual("lastname", sLastName);
            Assert.AreEqual(null, sNullString);
            Assert.AreEqual(string.Empty, sEmpty);
        }

        [Test]
        public void GetChildBool()
        {
            XmlElement XMLElement = (XmlElement)XMLDoc.DocumentElement.FirstChild;

            bool? bTrue = XMLElement.GetChildBool("has_added_app");
            bool? bFalse = XMLElement.GetChildBool("is_app_user");
            bool? bNull = XMLElement.GetChildBool("noneistent_element");
            bool? bEmpty = XMLElement.GetChildBool("empty_node");

            Assert.AreEqual(true, bTrue);
            Assert.AreEqual(false, bFalse);
            Assert.AreEqual(false, bNull);
            Assert.AreEqual(false, bEmpty);
        }

        [Test]
        public void GetChildLong()
        {
            XmlElement XMLElement = (XmlElement)XMLDoc.DocumentElement.FirstChild;

            long? lUID = XMLElement.GetChildLong("uid");
            long? lNullUID = XMLElement.GetChildLong("nonexistent_element");
            //long? lEmpty = XMLElement.GetChildLong("empty_node");

            Assert.AreEqual(111L, lUID);
            Assert.AreEqual(null, lNullUID);
        }

        [Test]
        public void GetChildInt()
        {
            XmlElement XMLElement = (XmlElement)XMLDoc.DocumentElement.FirstChild;

            int? nUID = XMLElement.GetChildInt("uid");
            int? nNullUID = XMLElement.GetChildInt("nonexistent_element");
            //int? lEmpty = XMLElement.GetChildInt("empty_node");

            Assert.AreEqual(111, nUID);
            Assert.AreEqual(null, nNullUID);
        }

        [Test]
        public void GetChildDouble()
        {
            XmlElement XMLElement = (XmlElement)XMLDoc.DocumentElement.FirstChild;

            double? dUID = XMLElement.GetChildDouble("uid");
            double? dNullUID = XMLElement.GetChildDouble("nonexistent_element");
           //double? dEmpty = XMLElement.GetChildDouble("empty_node");

            Assert.AreEqual(111D, dUID);
            Assert.AreEqual(null, dNullUID);
        }

        [Test]
        public void GetChildUnixTime()
        {
            XmlElement XMLElement = (XmlElement)XMLDoc.DocumentElement.FirstChild;

            DateTime? dtUnixTime = XMLElement.GetChildUnixTime("unix_time");
            DateTime? dtUnixTimeZero = XMLElement.GetChildUnixTime("unix_time_zero");
            DateTime? dtUnixTimeNeg = XMLElement.GetChildUnixTime("unix_time_neg");
            DateTime? dtNullDT = XMLElement.GetChildUnixTime("nonexistent_element");
      
            Assert.AreEqual(new DateTime(2008, 1, 7, 2, 49, 36), dtUnixTime);
            Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0), dtUnixTimeZero);
            Assert.AreEqual(new DateTime(1931, 12, 26, 21, 10, 24), dtUnixTimeNeg);
            Assert.AreEqual(null, dtNullDT);
        }
        
        [Test]
        public void GetFqlResults()
        {
            //test with keepxml bool set to true
            List<User> results = XMLDoc.GetFqlResults(true, e =>
                new User
                {
                    uid = long.Parse(e.ChildNodes[0].InnerText),
                    name = e.ChildNodes[3].InnerText,
                    pic = e.ChildNodes[7].InnerText
                });

            Assert.AreEqual(3, results.Count);
            Assert.AreEqual(111, results[0].uid);
            Assert.AreEqual("111", results[0].FqlResult.ChildNodes[0].InnerText);
            Assert.AreEqual("firstname lastname_2", results[1].name);
            Assert.AreEqual("firstname lastname_2", results[1].FqlResult.ChildNodes[3].InnerText);
            Assert.AreEqual("http://pic_link.jpg_3", results[2].pic);
            Assert.AreEqual("http://pic_link.jpg_3", results[2].FqlResult.ChildNodes[7].InnerText);

            //test with keepxml bool set to false
            results = XMLDoc.GetFqlResults(false, e =>
                new User
                {
                    uid = long.Parse(e.ChildNodes[0].InnerText),
                    name = e.ChildNodes[3].InnerText,
                    pic = e.ChildNodes[7].InnerText
                });

            Assert.AreEqual(3, results.Count);
            Assert.AreEqual(111, results[0].uid);
            Assert.AreEqual(null, results[0].FqlResult);
            Assert.AreEqual("firstname lastname_2", results[1].name);
            Assert.AreEqual(null, results[1].FqlResult);
            Assert.AreEqual("http://pic_link.jpg_3", results[2].pic);
            Assert.AreEqual(null, results[2].FqlResult);

            //test with nodes != XmlNodeType.Element?
        }
    }
}
